package com.fashionbrot.github.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

public class DecimalFormatUtil {

    private static final double z =0;

    private static final ThreadLocal<DecimalFormat> df = new ThreadLocal<DecimalFormat>() {

        @Override
        protected DecimalFormat initialValue() {
            DecimalFormat df = new DecimalFormat("0.00");
            df.setRoundingMode(RoundingMode.HALF_UP);
            return df;
        }
    };

    public static String convert(Double source){
        if (source==null){
            return "0.00";
        }
        return df.get().format(source);
    }

    public static String convert(BigDecimal source){
        if (source==null){
            return "0.00";
        }
        return df.get().format(source.doubleValue());
    }



    private static final ThreadLocal<DecimalFormat> df6 = new ThreadLocal<DecimalFormat>() {

        @Override
        protected DecimalFormat initialValue() {
            DecimalFormat df = new DecimalFormat("0.000000");
            df.setRoundingMode(RoundingMode.HALF_UP);
            return df;
        }
    };

    public static double convertToDouble(Double d){
        if (d==null){
            return z;
        }
        return StringUtil.parseDouble(df6.get().format(d));
    }

}
